#include "gUSBampDiag.h"

#ifdef HAVE_GTEC_USB_LIBRARY
BEGIN_EVENT_TABLE( gUsbAmpDiag, wxDialog )
	EVT_BUTTON(wxID_Save, gUsbAmpDiag::OnSave)
	EVT_BUTTON(wxID_Exit, gUsbAmpDiag::OnExit)
	EVT_BUTTON(wxID_APPLYCH, gUsbAmpDiag::OnApplyCh)
	EVT_BUTTON(wxID_APPLYSPEC, gUsbAmpDiag::OnApllySpec)
	EVT_CHOICE(wxID_SET, gUsbAmpDiag::OnSet)
	EVT_CHOICE(wxID_CHChoice,gUsbAmpDiag::OnChoice)
END_EVENT_TABLE()

gUsbAmpDiag::gUsbAmpDiag(wxWindow *parent, int *freq, UCHAR *nCh, UCHAR* _channelsToAcquire, gUSBamp::CHANNEL* _bipolarSettings, gUSBamp::REF* _commonReference, gUSBamp::GND* _commonGround, int *filter, int *nFilter, int numfilt, wxString *FilterString, int nnumfilt, wxString *nFilterString, gUSBamp::SCALE *scale) : gUsbAmpDialog(parent)
{
	pFreq = freq;
	pnCh=nCh;
	p_channelsToAcquire = _channelsToAcquire;
	p_bipolarSettings = _bipolarSettings;
	p_commonReference = _commonReference;
	p_commonGround = _commonGround;
	pFilter = filter;
	pnFilter = nFilter;
	pnumFilter = numfilt;
	pnnumFilter = nnumfilt;
	StringFilter = FilterString;
	nStringFilter = nFilterString;
	toAdd = 0;
	ntoAdd = 0;
	pScale = scale;
	
	//Initialize
	p_bipolarSettings->Channel1=0;
	p_bipolarSettings->Channel2=0;
	p_bipolarSettings->Channel3=0;
	p_bipolarSettings->Channel4=0;
	p_bipolarSettings->Channel5=0;
	p_bipolarSettings->Channel6=0;
	p_bipolarSettings->Channel7=0;
	p_bipolarSettings->Channel8=0;
	p_bipolarSettings->Channel9=0;
	p_bipolarSettings->Channel10=0;
	p_bipolarSettings->Channel11=0;
	p_bipolarSettings->Channel12=0;
	p_bipolarSettings->Channel13=0;
	p_bipolarSettings->Channel14=0;
	p_bipolarSettings->Channel15=0;
	p_bipolarSettings->Channel16=0;

	for (int i=0; i<16; i++)
	{
		pFilter[i] = 0;
		pnFilter[i] = 0;
	}
}

gUsbAmpDiag::~gUsbAmpDiag(void)
{
}

void gUsbAmpDiag::OnSave(wxCommandEvent& event)
{
	int cont=0;
	m_freq->GetString(m_freq->GetCurrentSelection()).ToLong((long*)pFreq);	
	
	//Common ground
	if (Gnd1->GetValue())
		p_commonGround->GND1=true;
	else
		p_commonGround->GND1=false;
	if (Gnd2->GetValue())
		p_commonGround->GND2=true;
	else
		p_commonGround->GND2=false;
	if (Gnd3->GetValue())
		p_commonGround->GND3=true;
	else
		p_commonGround->GND3=false;
	if (Gnd4->GetValue())
		p_commonGround->GND4=true;
	else
		p_commonGround->GND4=false;

	//Commonn reference
	if (Ref1->GetValue())
		p_commonReference->ref1=true;
	else
		p_commonReference->ref1=false;
	if (Ref2->GetValue())
		p_commonReference->ref2=true;
	else
		p_commonReference->ref2=false;
	if (Ref3->GetValue())
		p_commonReference->ref3=true;
	else
		p_commonReference->ref3=false;
	if (Ref4->GetValue())
		p_commonReference->ref4=true;
	else
		p_commonReference->ref4=false;

	if(selCh1->GetValue())
	{
		p_channelsToAcquire[cont]=1;
		cont++;
	}
	if(selCh2->GetValue())
	{
		p_channelsToAcquire[cont]=2;
		cont++;
	}
	if(selCh3->GetValue())
	{
		p_channelsToAcquire[cont]=3;
		cont++;
	}
	if(selCh4->GetValue())
	{
		p_channelsToAcquire[cont]=4;
		cont++;
	}
	if(selCh5->GetValue())
	{
		p_channelsToAcquire[cont]=5;
		cont++;
	}
	if(selCh6->GetValue())
	{
		p_channelsToAcquire[cont]=6;
		cont++;
	}
	if(selCh7->GetValue())
	{
		p_channelsToAcquire[cont]=7;
		cont++;
	}
	if(selCh8->GetValue())
	{
		p_channelsToAcquire[cont]=8;
		cont++;
	}
	if(selCh9->GetValue())
	{
		p_channelsToAcquire[cont]=9;
		cont++;
	}
	if(selCh10->GetValue())
	{
		p_channelsToAcquire[cont]=10;
		cont++;
	}
	if(selCh11->GetValue())
	{
		p_channelsToAcquire[cont]=11;
		cont++;
	}
	if(selCh12->GetValue())
	{
		p_channelsToAcquire[cont]=12;
		cont++;
	}
	if(selCh13->GetValue())
	{
		p_channelsToAcquire[cont]=13;
		cont++;
	}
	if(selCh14->GetValue())
	{
		p_channelsToAcquire[cont]=14;
		cont++;
	}
	if(selCh15->GetValue())
	{
		p_channelsToAcquire[cont]=15;
		cont++;
	}
	if(selCh16->GetValue())
	{
		p_channelsToAcquire[cont]=16;
		cont++;
	}
	*pnCh = cont;
	for (int i=0; i<cont; i++)
	{
		if (pFilter[i]==0)
			pFilter[i] = -1; //None
		else
			pFilter[i] = pFilter[i] + toAdd;
		if (pnFilter[i]==0)
			pnFilter[i] = -1; //None
		else
			pnFilter[i] = pnFilter[i] + ntoAdd;
	}

	this->Close();

}

void gUsbAmpDiag::OnExit(wxCommandEvent& event)
{
	*pFreq = NULL;
	this->Close();
}

void gUsbAmpDiag::OnApplyCh(wxCommandEvent& event)
{
	SpecCh->Clear();
	if(selCh1->GetValue())
	{
		SpecCh->AppendString(wxT("1"));
		p_bipolarSettings->Channel1=0;
	}
	if(selCh2->GetValue())
	{
		SpecCh->AppendString(wxT("2"));
		p_bipolarSettings->Channel2=0;
	}
	if(selCh3->GetValue())
	{
		SpecCh->AppendString(wxT("3"));
		p_bipolarSettings->Channel3=0;
	}
	if(selCh4->GetValue())
	{
		SpecCh->AppendString(wxT("4"));
		p_bipolarSettings->Channel4=0;
	}
	if(selCh5->GetValue())
	{
		SpecCh->AppendString(wxT("5"));
		p_bipolarSettings->Channel5=0;
	}
	if(selCh6->GetValue())
	{
		SpecCh->AppendString(wxT("6"));
		p_bipolarSettings->Channel6=0;
	}
	if(selCh7->GetValue())
	{
		SpecCh->AppendString(wxT("7"));
		p_bipolarSettings->Channel7=0;
	}
	if(selCh8->GetValue())
	{
		SpecCh->AppendString(wxT("8"));
		p_bipolarSettings->Channel8=0;
	}
	if(selCh9->GetValue())
	{
		SpecCh->AppendString(wxT("9"));
		p_bipolarSettings->Channel9=0;
	}
	if(selCh10->GetValue())
	{
		SpecCh->AppendString(wxT("10"));
		p_bipolarSettings->Channel10=0;
	}
	if(selCh11->GetValue())
	{
		SpecCh->AppendString(wxT("11"));
		p_bipolarSettings->Channel11=0;
	}
	if(selCh12->GetValue())
	{
		SpecCh->AppendString(wxT("12"));
		p_bipolarSettings->Channel12=0;
	}
	if(selCh13->GetValue())
	{
		SpecCh->AppendString(wxT("13"));
		p_bipolarSettings->Channel13=0;
	}
	if(selCh14->GetValue())
	{
		SpecCh->AppendString(wxT("14"));
		p_bipolarSettings->Channel14=0;
	}
	if(selCh15->GetValue())
	{
		SpecCh->AppendString(wxT("15"));
		p_bipolarSettings->Channel15=0;
	}
	if(selCh16->GetValue())
	{
		SpecCh->AppendString(wxT("16"));
		p_bipolarSettings->Channel16=0;
	}

	SpecCh->SetSelection(0);
	//Set filter
	mFilter1->Clear();
	mFilter1->AppendString(wxT("none"));
	m_freq->GetString(m_freq->GetCurrentSelection()).ToLong((long*)pFreq);	
	for (int i=0; i<pnumFilter; i++)
	{
		if (StringFilter[i].Contains(wxString::Format(wxT("%d"),*pFreq)))
		{
			mFilter1->AppendString(StringFilter[i]);
			toAdd = i;
		}
	}
	toAdd = toAdd-(mFilter1->GetCount()-1);

	mnFilter1->Clear();
	mnFilter1->AppendString(wxT("none"));
	m_freq->GetString(m_freq->GetCurrentSelection()).ToLong((long*)pFreq);	
	for (int i=0; i<pnnumFilter; i++)
	{
		if (nStringFilter[i].Contains(wxString::Format(wxT("%d"),*pFreq)))
		{
			mnFilter1->AppendString(nStringFilter[i]);
			ntoAdd = i;
		}
	}
	ntoAdd = ntoAdd-(mnFilter1->GetCount()-1);
	OnChoice(event);
}

void gUsbAmpDiag::OnApllySpec(wxCommandEvent& event)
{
	wxString temp,temp1;
	temp = SpecCh->GetStringSelection();
	long i, j;
	j = mFilter1->GetCurrentSelection();
	temp.ToLong(&i);
	pFilter[i-1] = j;
	j = mnFilter1->GetCurrentSelection();
	pnFilter[i-1] = j;
	
	temp1 = m_gain->GetLabel();
	temp1.ToLong(&j);
	pScale->factor[i-1]=j;
	temp1 = m_offset->GetLabel();
	temp1.ToLong(&j);
	pScale->offset[i-1]=j;

	temp1 = BipCh->GetStringSelection();
	temp1.ToLong(&j);
	switch (i)
	{
	case 1:	p_bipolarSettings->Channel1 = j;
		break;
	case 2:	p_bipolarSettings->Channel2 = j;
		break;
	case 3:	p_bipolarSettings->Channel3 = j;
		break;
	case 4:	p_bipolarSettings->Channel4 = j;
		break;
	case 5:	p_bipolarSettings->Channel5 = j;
		break;
	case 6:	p_bipolarSettings->Channel6 = j;
		break;
	case 7:	p_bipolarSettings->Channel7 = j;
		break;
	case 8:	p_bipolarSettings->Channel8 = j;
		break;
	case 9:	p_bipolarSettings->Channel9 = j;
		break;
	case 10:	p_bipolarSettings->Channel10 = j;
		break;
	case 11:	p_bipolarSettings->Channel11 = j;
		break;
	case 12:	p_bipolarSettings->Channel12 = j;
		break;
	case 13:	p_bipolarSettings->Channel13 = j;
		break;
	case 14:	p_bipolarSettings->Channel14 = j;
		break;
	case 15:	p_bipolarSettings->Channel15 = j;
		break;
	case 16:	p_bipolarSettings->Channel16 = j;
		break;
	}
}

void gUsbAmpDiag::OnSet(wxCommandEvent& event)
{
	switch(preset->GetCurrentSelection())
	{
	case 0:
		Gnd1->SetValue(false);
		Gnd2->SetValue(false);
		Gnd3->SetValue(false);

		Gnd4->SetValue(false);
		Ref1->SetValue(false);
		Ref2->SetValue(false);
		Ref3->SetValue(false);
		Ref4->SetValue(false);

		selCh1->SetValue(false);
		selCh2->SetValue(false);
		selCh3->SetValue(false);
		selCh4->SetValue(false);
		selCh5->SetValue(false);
		selCh6->SetValue(false);
		selCh7->SetValue(false);
		selCh8->SetValue(false);
		selCh9->SetValue(false);
		selCh10->SetValue(false);
		selCh11->SetValue(false);
		selCh12->SetValue(false);
		selCh13->SetValue(false);
		selCh14->SetValue(false);
		selCh15->SetValue(false);
		selCh16->SetValue(false);
		p_bipolarSettings->Channel1=0;
		p_bipolarSettings->Channel2=0;
		p_bipolarSettings->Channel3=0;
		p_bipolarSettings->Channel4=0;
		p_bipolarSettings->Channel5=0;
		p_bipolarSettings->Channel6=0;
		p_bipolarSettings->Channel7=0;
		p_bipolarSettings->Channel8=0;
		p_bipolarSettings->Channel9=0;
		p_bipolarSettings->Channel10=0;
		p_bipolarSettings->Channel11=0;
		p_bipolarSettings->Channel12=0;
		p_bipolarSettings->Channel13=0;
		p_bipolarSettings->Channel14=0;
		p_bipolarSettings->Channel15=0;
		p_bipolarSettings->Channel16=0;
		mFilter1->SetSelection(4);
		mnFilter1->SetSelection(1);
		for (int i=0; i<16; i++)
		{
			pFilter[i] = 0;
			pnFilter[i] = 0;
		}
		break;
	case 1: //EEG8CH mono
		Gnd1->SetValue(true);
		Gnd2->SetValue(true);
		Gnd3->SetValue(true);

		Gnd4->SetValue(true);
		Ref1->SetValue(true);
		Ref2->SetValue(true);
		Ref3->SetValue(true);
		Ref4->SetValue(true);

		selCh1->SetValue(true);
		selCh2->SetValue(true);
		selCh3->SetValue(true);
		selCh4->SetValue(true);
		selCh5->SetValue(true);
		selCh6->SetValue(true);
		selCh7->SetValue(true);
		selCh8->SetValue(true);
		selCh9->SetValue(false);
		selCh10->SetValue(false);
		selCh11->SetValue(false);
		selCh12->SetValue(false);
		selCh13->SetValue(false);
		selCh14->SetValue(false);
		selCh15->SetValue(false);
		selCh16->SetValue(false);
		p_bipolarSettings->Channel1=0;
		p_bipolarSettings->Channel2=0;
		p_bipolarSettings->Channel3=0;
		p_bipolarSettings->Channel4=0;
		p_bipolarSettings->Channel5=0;
		p_bipolarSettings->Channel6=0;
		p_bipolarSettings->Channel7=0;
		p_bipolarSettings->Channel8=0;
		p_bipolarSettings->Channel9=0;
		p_bipolarSettings->Channel10=0;
		p_bipolarSettings->Channel11=0;
		p_bipolarSettings->Channel12=0;
		p_bipolarSettings->Channel13=0;
		p_bipolarSettings->Channel14=0;
		p_bipolarSettings->Channel15=0;
		p_bipolarSettings->Channel16=0;
		mFilter1->SetSelection(4);
		mnFilter1->SetSelection(1);
		for (int i=0; i<16; i++)
		{
			pFilter[i] = 0;
			pnFilter[i] = 0;
		}
		break;
	case 2: //EEG8CH bipolar
		Gnd1->SetValue(true);
		Gnd2->SetValue(true);
		Gnd3->SetValue(true);
		Gnd4->SetValue(true);
		Ref1->SetValue(true);
		Ref2->SetValue(true);
		Ref3->SetValue(true);
		Ref4->SetValue(true);
		selCh1->SetValue(true);
		selCh2->SetValue(true);
		selCh3->SetValue(true);
		selCh4->SetValue(true);
		selCh5->SetValue(true);
		selCh6->SetValue(true);
		selCh7->SetValue(true);
		selCh8->SetValue(true);
		selCh9->SetValue(true);
		selCh10->SetValue(true);
		selCh11->SetValue(true);
		selCh12->SetValue(true);
		selCh13->SetValue(true);
		selCh14->SetValue(true);
		selCh15->SetValue(true);
		selCh16->SetValue(true);
		p_bipolarSettings->Channel1=2;
		p_bipolarSettings->Channel2=1;
		p_bipolarSettings->Channel3=4;
		p_bipolarSettings->Channel4=3;
		p_bipolarSettings->Channel5=6;
		p_bipolarSettings->Channel6=5;
		p_bipolarSettings->Channel7=8;
		p_bipolarSettings->Channel8=7;
		p_bipolarSettings->Channel9=10;
		p_bipolarSettings->Channel10=9;
		p_bipolarSettings->Channel11=12;
		p_bipolarSettings->Channel12=11;
		p_bipolarSettings->Channel13=14;
		p_bipolarSettings->Channel14=13;
		p_bipolarSettings->Channel15=16;
		p_bipolarSettings->Channel16=15;
		mFilter1->SetSelection(4);
		mnFilter1->SetSelection(1);
		for (int i=0; i<16; i++)
		{
			pFilter[i] = 0;
			pnFilter[i] = 0;
		}
		break;
	case 3: //EEG16CH mono
		Gnd1->SetValue(true);
		Gnd2->SetValue(true);
		Gnd3->SetValue(true);
		Gnd4->SetValue(true);
		Ref1->SetValue(true);
		Ref2->SetValue(true);
		Ref3->SetValue(true);
		Ref4->SetValue(true);
		selCh1->SetValue(true);
		selCh2->SetValue(true);
		selCh3->SetValue(true);
		selCh4->SetValue(true);
		selCh5->SetValue(true);
		selCh6->SetValue(true);
		selCh7->SetValue(true);
		selCh8->SetValue(true);
		selCh9->SetValue(true);
		selCh10->SetValue(true);
		selCh11->SetValue(true);
		selCh12->SetValue(true);
		selCh13->SetValue(true);
		selCh14->SetValue(true);
		selCh15->SetValue(true);
		selCh16->SetValue(true);
		p_bipolarSettings->Channel1=0;
		p_bipolarSettings->Channel2=0;
		p_bipolarSettings->Channel3=0;
		p_bipolarSettings->Channel4=0;
		p_bipolarSettings->Channel5=0;
		p_bipolarSettings->Channel6=0;
		p_bipolarSettings->Channel7=0;
		p_bipolarSettings->Channel8=0;
		p_bipolarSettings->Channel9=0;
		p_bipolarSettings->Channel10=0;
		p_bipolarSettings->Channel11=0;
		p_bipolarSettings->Channel12=0;
		p_bipolarSettings->Channel13=0;
		p_bipolarSettings->Channel14=0;
		p_bipolarSettings->Channel15=0;
		p_bipolarSettings->Channel16=0;
		mFilter1->SetSelection(4);
		mnFilter1->SetSelection(1);
		for (int i=0; i<16; i++)
		{
			pFilter[i] = 0;
			pnFilter[i] = 0;
		}
		break;
	case 4: //ECG8CH bip
		Gnd1->SetValue(true);
		Gnd2->SetValue(true);
		Gnd3->SetValue(true);
		Gnd4->SetValue(true);
		Ref1->SetValue(true);
		Ref2->SetValue(true);
		Ref3->SetValue(true);
		Ref4->SetValue(true);
		selCh1->SetValue(true);
		selCh2->SetValue(true);
		selCh3->SetValue(true);
		selCh4->SetValue(true);
		selCh5->SetValue(true);
		selCh6->SetValue(true);
		selCh7->SetValue(true);
		selCh8->SetValue(true);
		selCh9->SetValue(true);
		selCh10->SetValue(true);
		selCh11->SetValue(true);
		selCh12->SetValue(true);
		selCh13->SetValue(true);
		selCh14->SetValue(true);
		selCh15->SetValue(true);
		selCh16->SetValue(true);
		p_bipolarSettings->Channel1=2;
		p_bipolarSettings->Channel2=1;
		p_bipolarSettings->Channel3=4;
		p_bipolarSettings->Channel4=3;
		p_bipolarSettings->Channel5=6;
		p_bipolarSettings->Channel6=5;
		p_bipolarSettings->Channel7=8;
		p_bipolarSettings->Channel8=7;
		p_bipolarSettings->Channel9=10;
		p_bipolarSettings->Channel10=9;
		p_bipolarSettings->Channel11=12;
		p_bipolarSettings->Channel12=11;
		p_bipolarSettings->Channel13=14;
		p_bipolarSettings->Channel14=13;
		p_bipolarSettings->Channel15=16;
		p_bipolarSettings->Channel16=15;
		mFilter1->SetSelection(5);
		mnFilter1->SetSelection(1);
		for (int i=0; i<16; i++)
		{
			pFilter[i] = 0;
			pnFilter[i] = 0;
		}
		break;
	}
	//Set Scales
	for (int i=0; i<16; i++)
	{
		pScale->factor[i] = 1;
		pScale->offset[i] = 0;
	}
	OnApplyCh(event);
	CheckCh();
}


void gUsbAmpDiag::OnChoice(wxCommandEvent& event)
{
	wxString temp;
	BipCh->Clear();
	BipCh->AppendString(wxT("0"));
	int num = SpecCh->GetCount();
	temp = SpecCh->GetStringSelection();
	for (int i=0; i<num; i++)
	{
		if (i!=SpecCh->FindString(temp))
			BipCh->AppendString(SpecCh->GetString(i));
	}
	CheckCh();
}

void gUsbAmpDiag::CheckCh(void)
{
	wxString temp = SpecCh->GetStringSelection();
	unsigned int a;
	long ch;
	temp.ToLong(&ch);
	ch = ch-1;
	switch (ch)
	{
	case 0:
		a = p_bipolarSettings->Channel1;
		break;
	case 1:
		a = p_bipolarSettings->Channel2;
		break;
	case 2:
		a = p_bipolarSettings->Channel3;
		break;
	case 3:
		a = p_bipolarSettings->Channel4;
		break;
	case 4:
		a = p_bipolarSettings->Channel5;
		break;
	case 5:
		a = p_bipolarSettings->Channel6;
		break;
	case 6:
		a = p_bipolarSettings->Channel7;
		break;
	case 7:
		a = p_bipolarSettings->Channel8;
		break;
	case 8:
		a = p_bipolarSettings->Channel9;
		break;
	case 9:
		a = p_bipolarSettings->Channel10;
		break;
	case 10:
		a = p_bipolarSettings->Channel11;
		break;
	case 11:
		a = p_bipolarSettings->Channel12;
		break;
	case 12:
		a = p_bipolarSettings->Channel13;
		break;
	case 13:
		a = p_bipolarSettings->Channel14;
		break;
	case 14:
		a = p_bipolarSettings->Channel15;
		break;
	case 15:
		a = p_bipolarSettings->Channel16;
		break;
	}
	
	mFilter1->SetSelection(pFilter[ch]);
	mnFilter1->SetSelection(pnFilter[ch]);
	
	int num = BipCh->FindString(wxString::Format(wxT("%d"),a));
	BipCh->SetSelection(num);
	
	m_gain->SetLabel(wxString::Format(wxT("%.2f"),pScale->factor[ch]));
	m_offset->SetLabel(wxString::Format(wxT("%.2f"),pScale->offset[ch]));
}

#endif
